package com.pro.service.imple;

import com.pro.dao.MicrocircleMapper;
import com.pro.entity.*;
import com.pro.service.MicrocircleService;
import com.pro.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MicrocircleServiceImple implements MicrocircleService {

    @Autowired
    private MicrocircleMapper microcircleMapper;

    @Override
    public List<JsActicleCategroy> getJsActicleCategroy(Integer acticleCategroyId) {
        JsActicleCategroy jsActicleCategroy = new JsActicleCategroy();
        jsActicleCategroy.setActicleCategroyId(50);
        jsActicleCategroy.setArtcleCategroyName("选择分类类型");
        jsActicleCategroy.setParentId(0);
        List<JsActicleCategroy> jsActicleCategroy1 = microcircleMapper.getJsActicleCategroy(acticleCategroyId);
        jsActicleCategroy1.add(jsActicleCategroy);
        return jsActicleCategroy1;
    }

    @Override
    public boolean addZtree(JsActicleCategroy jsActicleCategroy) {
        jsActicleCategroy.setCreateDate(new Date());
        boolean b = microcircleMapper.addZtree(jsActicleCategroy);
        return b;
    }

    @Override
    public boolean delZtree(Integer acticleCategroyId) {
        boolean b = microcircleMapper.delZtree(acticleCategroyId);
        return b;
    }

    @Override
    public boolean editZtree(String artcleCategroyName, Integer acticleCategroyId) {
        boolean b = microcircleMapper.editZtree(artcleCategroyName, acticleCategroyId);
        return b;
    }

    @Override
    public List<JsMicrocircle> getMicrocircleList(JsMicrocircle jsMicrocircle) {
        List<JsMicrocircle> microcircleList = microcircleMapper.getMicrocircleList(jsMicrocircle);
        return microcircleList;
    }

    @Override
    public Integer getCollectionNum(Integer microcircleId) {
        Integer collectionNum = microcircleMapper.getCollectionNum(microcircleId);
        return collectionNum;
    }

    @Override
    public boolean delMicroCircle(Integer microcircleId) {
        boolean b = microcircleMapper.delMicroCircle(microcircleId);
        return b;
    }

    @Override
    public boolean addLog(JsLog jsLog) {
        jsLog.setCreateDate(new Date());
        boolean b = microcircleMapper.addLog(jsLog);
        return b;
    }

    @Override
    public List<JsLog> selLog(JsLog jsLog) {
        List<JsLog> jsLogs = microcircleMapper.selLog(jsLog);
        return jsLogs;
    }

    @Override
    public boolean delLog(Integer logId) {
        boolean b = microcircleMapper.delLog(logId);
        return b;
    }

    @Override
    public JsMicrocircle getDraftMicrocircle(Integer details) {
        JsMicrocircle draftMicrocircle = microcircleMapper.getDraftMicrocircle(details);
        return draftMicrocircle;
    }

    @Override
    public boolean upDraftMicrocircle(JsMicrocircle jsMicrocircle) {
        jsMicrocircle.setUpdateDate(new Date());
        boolean b = microcircleMapper.upDraftMicrocircle(jsMicrocircle);
        return b;
    }

    @Override
    public boolean addDraftMirList(JsMicrocircle jsMicrocircle) {
        boolean b = microcircleMapper.addDraftMirList(jsMicrocircle);
        return b;
    }

    @Override
    @Transactional
    public boolean addMirImg(JsMicrocircleImg jsMicrocircleImg) {
        jsMicrocircleImg.setCreateDate(new Date());
        jsMicrocircleImg.setUpdateDate(jsMicrocircleImg.getCreateDate());
        boolean b = microcircleMapper.addMirImg(jsMicrocircleImg);
        if (b){
            JsCourseRecord jsCourseRecord = new JsCourseRecord();
            jsCourseRecord.setCourseId(jsMicrocircleImg.getProductId());
            jsCourseRecord.setApplyDate(jsMicrocircleImg.getCreateDate());
            jsCourseRecord.setLaunchId(jsMicrocircleImg.getCreateId());
            jsCourseRecord.setRemarks("微圈审核");
            boolean b1 = microcircleMapper.addMirExamine(jsCourseRecord);
            if (b1){
                JsMicrocircle jsMicrocircle = new JsMicrocircle();
                jsMicrocircle.setReleaseDate(jsMicrocircleImg.getCreateDate());
                jsMicrocircle.setMicrocircleId(jsMicrocircleImg.getProductId());
                boolean b2 = microcircleMapper.upDraft(jsMicrocircle);
                return b2;
            }
        }
        return false;
    }

    @Override
    public boolean addMirList(JsMicrocircle jsMicrocircle) {
        jsMicrocircle.setUpdateDate(new Date());
        jsMicrocircle.setCreateDate(jsMicrocircle.getUpdateDate());
        jsMicrocircle.setReleaseDate(jsMicrocircle.getUpdateDate());
        jsMicrocircle.setArtcleKeywords("微圈");
        boolean b = microcircleMapper.addMirList(jsMicrocircle);
        if (b){
            JsMicrocircleImg jsMicrocircleImg = new JsMicrocircleImg();
            jsMicrocircleImg.setCreateDate(jsMicrocircle.getUpdateDate());
            jsMicrocircleImg.setProductId(jsMicrocircle.getMicrocircleId());
            jsMicrocircleImg.setMicrocircleImgUrl(jsMicrocircle.getArtcleUrl());
            jsMicrocircleImg.setMicrocircleImgType(3);
            jsMicrocircleImg.setCreateId(String.valueOf(jsMicrocircle.getUserId()));
            jsMicrocircleImg.setCreateDate(jsMicrocircle.getUpdateDate());
            jsMicrocircleImg.setUpdateId(String.valueOf(jsMicrocircle.getUserId()));
            jsMicrocircleImg.setUpdateDate(jsMicrocircle.getUpdateDate());
            boolean b1 = microcircleMapper.addMirImg(jsMicrocircleImg);
            if (b1){
                JsCourseRecord jsCourseRecord = new JsCourseRecord();
                jsCourseRecord.setCourseId(jsMicrocircle.getMicrocircleId());
                jsCourseRecord.setApplyDate(jsMicrocircle.getUpdateDate());
                jsCourseRecord.setLaunchId(String.valueOf(jsMicrocircle.getUserId()));
                jsCourseRecord.setRemarks("微圈审核");
                boolean b2 = microcircleMapper.addMirExamine(jsCourseRecord);
                return b2;
            }
        }
        return false;
    }

    @Override
    public JsActicleCategroy queryMicroCircleClassification(Integer acticleCategroyId) {
        JsActicleCategroy jsActicleCategroy = microcircleMapper.queryMicroCircleClassification(acticleCategroyId);
        return jsActicleCategroy;
    }

    @Override
    public List<JsComment> getMicroCircleComment(Integer commodityId) {
        List<JsComment> microCircleComment = microcircleMapper.getMicroCircleComment(commodityId);
        List<JsComment> listFilter = SensitiveWordsUtil.getListFilter(microCircleComment);
        return listFilter;
    }

    @Override
    public boolean hideComment(Integer id,Integer display) {
        boolean b = microcircleMapper.hideComment(id,display);
        return b;
    }

    @Override
    public boolean delComment(Integer id) {
        boolean b = microcircleMapper.delComment(id);
        return b;
    }

    @Override
    public boolean addMicroCircleComment(JsComment jsComment) {
        jsComment.setParentId(0);
        jsComment.setCommentDate(new Date());
        jsComment.setCreateDate(jsComment.getCommentDate());
        jsComment.setUpdateDate(jsComment.getCommentDate());
        boolean b = microcircleMapper.addMicroCircleComment(jsComment);
        return b;
    }

    @Override
    public List<JsMicrocircle> getStatistics() {
        List<Integer> list = microcircleMapper.getMicroCircleClassification();
        List<JsMicrocircle> hitsLikeNums = new ArrayList<>();
        for (Integer li : list){
            JsMicrocircle hitsLikeNums1 = microcircleMapper.getHitsLikeNums(li);
            Integer collectionNum = microcircleMapper.getCollectionNums(li);
            hitsLikeNums1.setCollectionNum(collectionNum);
            if (hitsLikeNums1.getActicleLike() == null){
                hitsLikeNums1.setActicleLike(0);
            }
            if (hitsLikeNums1.getActicleHits() == null){
                hitsLikeNums1.setActicleHits(0);
            }
            if (hitsLikeNums1.getCollectionNum() == null){
                hitsLikeNums1.setCollectionNum(0);
            }
            hitsLikeNums.add(hitsLikeNums1);
        }
        return hitsLikeNums;
    }

    @Override
    public boolean delImgPreview(Integer microcircleId) {
        boolean b = microcircleMapper.delImgPreview(microcircleId);
        return b;
    }

}
